package com.example.wanandroid.ui.fragment;

import static android.content.ContentValues.TAG;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.wanandroid.R;
import com.example.wanandroid.core.bean.BaseResponse;
import com.example.wanandroid.core.bean.project.ProjectClassifyData;
import com.example.wanandroid.core.http.api.Apis;
import com.example.wanandroid.core.utils.HttpUtil;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author jiangyi
 */
public class ProjectListFragment extends BaseFragment{
    TabLayout mTableLayout;
    private ViewPager mViewPager;
    private MyAdapter adapter;

    SlidingTabLayout mTab;

    List<ProjectClassifyData> projectListItemData;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getProjectItemList();
    }
    @Override
    protected void showLoading() {

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //绑定一下fragment资源文件以及生成fragment对象
        View view = inflater.inflate(R.layout.project_list_fragment,null);

        mViewPager = view.findViewById(R.id.project_list_pager);

        mTab = view.findViewById(R.id.tab_fragment);

        //返回视图
        return view;
    }

    private void getProjectItemList() {
        //通过simpleAdapter实现将网络请求来的数据绑定到view上，图片地址数据要做特殊处理
        //创建网络请求接口对象实例
        Apis api = HttpUtil.getApi();
        //对发送请求进行封装，传入接口参数
        Call<BaseResponse<List<ProjectClassifyData>>> jsonData = api.getProjectClassifyData();

        //发送网络请求(异步)
        jsonData.enqueue(new Callback<BaseResponse<List<ProjectClassifyData>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<ProjectClassifyData>>> call, Response<BaseResponse<List<ProjectClassifyData>>> response) {

                BaseResponse<List<ProjectClassifyData>> body = response.body();
                if(body==null) return;
                projectListItemData = body.getData();
                if(projectListItemData==null) return;
                adapter = new MyAdapter(getFragmentManager());
                mViewPager.setAdapter(adapter);

                mTab.setViewPager(mViewPager);
            }

            @Override
            public void onFailure(Call<BaseResponse<List<ProjectClassifyData>>> call, Throwable t) {
                Log.e(TAG, "get回调失败：" + t.getMessage() + "," + t.toString());
            }
        });
    }

    //创建Fragment的适配器
    class MyAdapter extends FragmentPagerAdapter {
//        String[] titles = { "全部", "待付款", "待发货", "待收货", "待评价" };

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }
        //获得每个页面的下标
        @Override
        public Fragment getItem(int position) {
            ProjectItemListFragment fragment = new ProjectItemListFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("id", projectListItemData.get(position).getId());
            fragment.setArguments(bundle);
            return fragment;
        }
        //获得List的大小
        @Override
        public int getCount() {
            return projectListItemData.size();
        }
        //获取title的下标
        @Override
        public CharSequence getPageTitle(int position) {
            return projectListItemData.get(position).getName();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}
